<?php
/**
 * 流量记录模型
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */
/**
rights Reserved, Designed By www.youyacao.com
@author:成都市一颗优雅草科技有限公司
@version 优雅草蜻蜓系统-服务端部分
注意：服务端代码在获得授权之前通过其他非官方渠道获得代码均为侵权，禁止用于商业用途，否则将承担因此带来等版权纠纷。
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:2853810243 422108995 23625059584
优雅草开源代码地址：https://gitee.com/youyacao  可查阅所有开源项目



 */
namespace App\Models;

class View extends BaseModel
{
    protected $table = 'view';

    protected $fillable = [
        'data_type',
        'data_id',
        'user_id',
        'device_id',
        'remark',
        'status'
    ];

    // 浏览次数
    public static function view($data_type, $data_id, $user_id = 0, $device_id = '', $remark = '')
    {
        $res = false;
        switch ($data_type) {
            case 1: $res = Video::where('id', $data_id)->increment('view_num');break;
            case 2: $res = Article::where('id', $data_id)->increment('view_num');break;
            case 3: $res = Live::where('id', $data_id)->increment('view_num');break;
            case 4: $res = Advert::where('id', $data_id)->increment('view_num');break;
            case 5: $res = Topic::where('id', $data_id)->increment('view_num');break;
            case 6: $res = Movie::where('id', $data_id)->increment('view_num');break;
        }
        if (!$res) {
            return false;
        }
        if ($user_id) {
            self::create([
                'data_type' => $data_type,
                'data_id'   => $data_id,
                'user_id'   => $user_id,
                'device_id' => $device_id ? $device_id:request()->ip(),
                'remark'    => $remark,
                'status'    => 1
            ]);
        }
        return true;
    }
}
